/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: peer.h
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __MLSE_NETWORK_PEER_H__
#define	__MLSE_NETWORK_PEER_H__

#include <network/network.h>
#include <script/script.h>
#include <tools/http.h>

#define __MLSE_NOIO_RECV_BUFFSIZE__ 	512


class CPeer : public IPeer
{
public:
    CPeer();
    CPeer(ISocket *pSocket, SOCKET sck = INVALID_SOCKET);
    void Init(ISocket *pSocket, SOCKET sck = INVALID_SOCKET);

    virtual ~CPeer();
    virtual void Destory();
    virtual bool Connect(const char *szAddr, unsigned int uPort);
	unsigned int OnWriteReady(unsigned int nSent);
    virtual void OnReadReady(LPPACKET pPacket) {};
    virtual void OnError() { m_pSocket->Close(m_raw_sck); };
    virtual NETWORKBUFFER *GetRecvBuffer()
    {
        return &m_recvbuffer;
    };
    virtual NETWORKBUFFER *GetSendBuffer()
    {
        return &m_sendbuffer;
    };
    virtual void OnEvent(LPEVENT ev, int event_sets);
    virtual int SendData(char *, unsigned int);
    virtual int RecvData();
	int RecvHttpData();

    virtual HANDLE GetIOHandle() { return (HANDLE)m_raw_sck; };
    virtual int GetId() { return (int)m_raw_sck; };
    virtual void SetMachine(IScriptMachine *pMachine){ m_pMachine = pMachine; };
    virtual void SetDataType(char c){m_data_tp = c;};
    virtual void ProcessHttpRequest(CMapWrapper *, LPHTTPRESPONSE, LPNETWORKBUFFER){};

protected:
	int RecvFromFd();
	int PostNextRecv();
protected:
	bool m_isio;
    ISocket *m_pSocket;
    SOCKET m_raw_sck;
	IScriptMachine *m_pMachine;
    NETWORKBUFFER m_recvbuffer;
    NETWORKBUFFER m_sendbuffer;

	HTTP_ACTION m_ha;
	CAutoExpandBuffer<255> m_form_data;
	char m_data_tp;
    static NETWORKBUFFER m_buffer;
};

#endif
